﻿@{
    ViewBag.Title = "Manage";
}
<script>
    var contentname = '@ViewData["name"]';

    $(document).ready(function () {
        if (contentname.length !== 0) {
            // Show loading animation
            showLoadingAnimation($('#grid-container .grid-items-list'));
            getData('/h/' + contentname, '', loadData, false, '', $('#grid-container .grid-items-list'), true);
        }
        else {
            $('#grid-container').epicGrid({ contenttype: contentname });
        }
    });

    function refreshData() {
        // Show loading animation
        showLoadingAnimation($('#grid-container .grid-items-list'));
        getData('/h/' + contentname, '', insertData, false, '', $('#grid-container .grid-items-list'), true);
    }

    function loadData(data, innercontainer) {
        insertData(data, innercontainer);

        $('#grid-container').epicGrid({ contenttype: contentname, refreshFunction: refreshData,
            addFunction: addCreatorItem, editFunction : editItems, deleteFunction: deleteItems});
    }

    function addCreatorItem() {
        var newDivId = "new" + contentname + $('#editor').find('div.add-div').length;
        $('#editor').append('<div class="add-div"></div>');
        $.get("/Create/d/" + contentname + "?embedded=true&target=" + newDivId, function (html) {           
            $('#editor .add-div:last').html(html);
        });
    }

    function editItems() {
        var selectedItems = $('.checked-grid-item-bar');
        $.each(selectedItems, function (i, item) {
            var itemID = $(item).find('.id-tag').html().trim();
            addEditorItem(itemID);
        });
    }

    function deleteItems() {
        var selectedItems = $('.checked-grid-item-bar');
        $.each(selectedItems, function (i, item) {
            var itemID = $(item).find('.id-tag').html().trim();
            getData('/m/sp_deleteContentById', 'id=' + itemID, function () { }, false);
        });
    }

    function addEditorItem(id) {
        var newDivId = "edit" + contentname + id;
        if ($('#' + newDivId).length != 0)
            return;
        $('#editor').append('<div class="edit-div ' + newDivId +'"></div>');
        $('#editor div.edit-div.' + newDivId).load("/Edit/d/" + contentname + "/" + id + "/?embedded=true&loadscripts=false&target=" + newDivId);
        //$.get("/Edit/" + contentname + "/" + id + "/?embedded=true&loadscripts=false&target=" + newDivId, function (html) {
        //    $('div.edit-div.' + newDivId).html(html);
        //});
    }
</script>

@{Html.RenderPartial("EpicGrid", null);}